<?php


/**
 * 文章管理
 * Enter description here ...
 * @author Admin
 *
 */
class ArticleAction extends CommonAction{
	
	
	/**
	 * 文章管理列表首页
	 * Enter description here ...
	 */
	public function index() {
		$article = D('Article');  
		$prefix = C('DB_PREFIX');   //数据库表前缀
		
		import("@.ORG.Page"); //导入分页类
		$count = $article->count();    //计算总数
		$p = new Page ($count,10);
		
		$articledata = $article->Table("{$prefix}article as A")->
					  join("{$prefix}article_categories as AC ON A.cid = AC.id")->
					  field('A.*,AC.name as categories')->
					  limit($p->firstRow.','.$p->listRows)-> 
					  order('id desc')->findAll();
		$page = $p->show();  //分页查询
		
		$this->assign("page",$page);
		$this->assign("articledata",$articledata);
		
		$this->display();
	}
	
	
	
	/**
	 * 新建文章
	 * @see CommonAction::add()
	 */
	public function add(){
		$article_categories = D('Article_categories');
		$article_categoriesdata = $article_categories->select();   //所有文章分类
		 
		$this->assign("article_categoriesdata",$article_categoriesdata);
		
		parent::add();
	}
	
	
	
	/**
	 * 新建文章保存
	 * @see CommonAction::insert()
	 */
	public function insert(){
		$data = Init_GP(array('title','cid','link','order','keywords','description','state'));
		
		$data['body']=HtmlReplace($_POST['body']);  //新闻内容大文本
		
		$article = D('Article');
		
		$data = $article->create($data);
		if(empty($data) || $data === false) {
			$this->error($article->getError());
		}
		$id = $article->add($data);  //保存文章
		
		
		if($id) { //保存成功
			$v = Init_GP(array('imgs'));       //相关图片
			$imgs = $v['imgs'];
			
			if (!empty($imgs)){
				
				$accessory_relation = M('Accessory_relation');  //图片附件业务应用关联表
				$accessory_relationdata['relationid'] =$id;  //业务表主键
				$accessory_relationdata['table'] = "Article"; //业务表名称
				
				foreach ($imgs as $val){
					$accessory_relationdata['accessoryid'] = $val;     //图片附件表主键
					
					$value = $accessory_relation->create($accessory_relationdata);
					$accessory_relation->add($value);
					
					unset($accessory_relationdata['accessoryid']);
				}
				
			}
			
			$this->assign('jumpUrl','__URL__/index');
			$this->success("添加成功");
		}else {
			//失败提示
			$this->error($article->getError());
		}
	}
	
	
	
	
	/**
	 * 修改文章
	 * @see CommonAction::edit()
	 */
	public function edit(){
		$article	= D('Article');
		$id     = GetNum($_GET['id']);   //主键
		$vo	=	$article->find($id);
		
		//附件数据
		$accessory_relation = M('Accessory_relation');  //图片附件业务应用关联表
		
		$prefix = C('DB_PREFIX');  //数据表前缀
		
		$accessorydata = $accessory_relation->Table("{$prefix}accessory_relation as A")->
					  join("{$prefix}accessory as B ON A.accessoryid = B.id")->
					  field('B.*')->
					  where("A.relationid = {$id} and A.table = 'Article'")->
					  findAll();
					  
		$article_categories = D('Article_categories');   //文章分类
		$article_categoriesdata = $article_categories->select();
		
		$this->assign("article_categoriesdata",$article_categoriesdata);
		$this->assign('data',$vo);
		$this->assign('accessorydata',$accessorydata);
		
		$this->display();
	}
	
	
	/**
	 * 修改文章确认
	 * @see CommonAction::update()
	 */
	public function update(){
		$id = GetNum($_POST['id']);  //主键ID
		
		$awhere['id'] = $id;
		$where['relationid'] = array('eq',$id);
		$where['table'] = array('eq','Article');
		
		$data = Init_GP(array('title','cid','link','order','keywords','description','state'));
		$data['body']=HtmlReplace($_POST['body']);   //文章内容 大文本
		
		//删除旧关系
		$accessory_relation = M('Accessory_relation');
		$accessory_relation->where($where)->delete();
		
		//是否有图片
		$v = Init_GP(array('imgs'));
		$imgs = $v['imgs'];
		
		//添加新关系
		if (!empty($imgs)){
			
				$accessory_relationdata['relationid'] =$id;
				$accessory_relationdata['table'] = "Article";  //图片附件业务关联表
				
				foreach ($imgs as $val){  //保存图片业务关联记录
					$accessory_relationdata['accessoryid'] = $val;
					$value = $accessory_relation->create($accessory_relationdata);
					$accessory_relation->add($value);
					
					unset($accessory_relationdata['accessoryid']);
				}
		}
		
		
		parent::update($data,$awhere);  //修改文章内容
	}
	
	
	
	/**
	 * 删除文章（单条）
	 * @see CommonAction::del()
	 */
	public function del(){
		$id = GetNum($_GET['id']);  //主键
		
		$where['relationid'] = array('eq',$id);
		$where['table'] = array('eq','Article');
		
		$accessory_relation = M('Accessory_relation');
		$accessory_relation->where($where)->delete();  //删除图片
		
		parent::del($id);   //删除文章
	}
	
	
	/**
	 * 删除文章（多条）
	 * @see CommonAction::del()
	 */
	public function dels(){
		$data = Init_GP(array('ids'));
		$ids = $data['ids'];  //多个主键
		
		$tmp       = getdotstring($ids,'int');
		
		$where['relationid'] = array('in',$tmp);
		$where['table'] = array('eq','Article');
		
		$accessory_relation = M('Accessory_relation');
		$accessory_relation->where($where)->delete();  //删除图片
		
		
		parent::dels($ids);  //删除文章
	}
	
	
	
}
?>